package dao;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Dao factory for every model
 * 
 * Every Dao class should inherit this one
 * @author MUSAB, Sukhjinder, Buthainah
 *
 */
public class DaoFactory {
	
	// Database name
	protected static final String dbName = "db.db";
	
	
	// Database path
	protected static String dbPath = System.getProperty("user.dir") + "/persistent/";
	
	// Connection
	protected static Connection conn;
	
	/**
	 * Default constructor, creates the database for the first time of app launch
	 * @author Musab M. Mirza
	 * @throws SQLException 
	 * @since May 9, 2013
	 */
	public DaoFactory() throws SQLException{
		
    	// Creates the persistent folder if it doesn't exist yet
		File file = new File(dbPath);
		if(!file.exists()){
			file.mkdir();
		}
		
		// Inits the database 
    	if(!(new File(dbPath+dbName).exists())) {
    		initDb();
        	setupTables();
    	}
    	
    	
	}

	

	/**
	 * Creates the dabase file
	 * @throws SQLException
	 */
	private void initDb() throws SQLException{
		try {
			Class.forName("org.sqlite.JDBC");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection("jdbc:sqlite:"+dbPath+dbName);
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}
	
	/**
	 * Sets up tables for basic use. This line is always run, so it should always have
	 * the   
	 */
	private void setupTables(){

		Statement statement = null;
		
		// Create user table
		String sql = "CREATE TABLE users("
				+ "Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
				+ "Name VARCHAR(20) NOT NULL"
				+ ")";
		try {
			statement = conn.createStatement();
			statement.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// Create folder table
		sql = "CREATE TABLE folders("
				+ "Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
				+ "Name VARCHAR(20) NOT NULL, "
				+ "UserId SMALLINT NOT NULL, "
				+ "FOREIGN KEY(UserId) REFERENCES users(Id) "
				+ ")";
		try {
			statement = conn.createStatement();
			statement.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// Create email table
		sql = "CREATE TABLE emails("
				+ "Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
				+ "ToEmail VARCHAR(50) NOT NULL, "
				+ "FromEmail VARCHAR(50) NOT NULL, "
				+ "Subject VARCHAR(50), "
				+ "Body TEXT, "
				+ "Read BOOLEAN, "
				+ "Ts INTEGER NOT NULL, "
				+ "FolderId SMALLINT NOT NULL," 
				+ "FOREIGN KEY(FolderId) REFERENCES folders(Id) "
				+ ")";
		try {
			statement = conn.createStatement();
			statement.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	
    public static EmailDao getEmailDao() {
        return new EmailSqlDao(conn);
    }

    public static UserDao getUserDao() {
        return new UserSqlDao(conn);
    }
    
    public static FolderDao getFolderDao() {
        return new FolderSqlDao(conn);
    }
    
}

